package com.huifaapp.mall.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.huifaapp.mall.APP;
import com.huifaapp.mall.js.BdConfig;
import com.huifaapp.mall.uitils.ToastUtil;


/**
 * Created by chen on 2015/10/14.
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected abstract int getLayoutId();
    protected abstract void initialize();
    protected abstract void initView();

    public APP application;
    public BaseActivity that;


    protected Handler mHandler = new Handler();



    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        /**
         * 去除应用标题
         */
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }
        application = (APP) getApplication();
        that = this;
        initialize();
        initView();
    }


    public void showMsg(Object msg){
        if (msg == null) {
            return;
        }
        ToastUtil.showL(that, String.valueOf(msg));
    }


    /**
     * 隐藏输入法
     */
    public void hideInputMethod(){
        InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if(manager.isActive()){
            View focusView = getCurrentFocus();
            if(focusView == null){
                return;
            }
            manager.hideSoftInputFromWindow(focusView.getWindowToken(), 0);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void finish() {
        hideInputMethod();
        super.finish();
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    public void debugError(Object msg) {
        if (BdConfig.isDebug()) {
            if(msg instanceof String && String.valueOf(msg).length() > 4000) {
                subLog(String.valueOf(msg), this.getClass().getSimpleName());
            } else {
                Log.e(this.getClass().getSimpleName(), String.valueOf(msg));
            }
        }
    }

    /**
     * layoutId获取对象
     * @param layoutId
     * @return
     */
    public View getLayoutView(int layoutId) {
        return LayoutInflater.from(getApplicationContext()).inflate(layoutId, null);
    }

    public void goTargetActivity(Class<?> cls){
        Intent intent = new Intent(BaseActivity.this, cls);
        startActivity(intent);
    }

    public void goTargetAndFinish(Class<?> cls){
        goTargetActivity(cls);
        finish();
    }



    @Override
    public void startActivity(Intent intent) {
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
        super.startActivity(intent);
    }

    @Override
    public void startActivity(Intent intent, Bundle bundle) {
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
        super.startActivity(intent, bundle);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
        super.startActivityForResult(intent, requestCode);
    }

    public void goBack(View v) {
        finish();
    }



    public static void subLog(String str, String tag){
        try {
            int index = 0;
            int maxLength = 4000;
            String sub;
            while (index < str.length()) {
                // java的字符不允许指定超过总的长度end
                if (str.length() <= index + maxLength) {
                    sub = str.substring(index);
                } else {
                    sub = str.substring(index, Math.min(maxLength + index, str.length()));
                }
                index += maxLength;
                Log.e(tag, sub);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void post(Runnable r){
        mHandler.post(r);
    }

    public void postDelayed(Runnable r, long delayMillis){
        mHandler.postDelayed(r, delayMillis);
    }

    public void removeCallbacks(Runnable r){
        mHandler.removeCallbacks(r);
    }

    public static class SimpleTextChangedListener implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
        @Override
        public void afterTextChanged(Editable s) {}
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 获取屏幕宽
     * @return
     */
    public int getScreenWidth(){
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric.widthPixels;
    }

    /**
     * 获取屏幕高
     * @return
     */
    public int getScreenHeight(){
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric.heightPixels;
    }
    /**
     * 输出调试信息
     * @param
     */
    public void debugInfo(Object msg) {
        if (BdConfig.isDebug()) {
            Log.i(this.getClass().getSimpleName(), String.valueOf(msg));
        }
    }

}
